import { Injectable } from '@nestjs/common'
import { PrismaService } from '../prisma/prisma.service'
import { CreateCommentDto } from './dto/create-web-comment.dto'

@Injectable()
export class WebCommentService {
    constructor(private readonly prisma: PrismaService) {}

    getCount() {
        return this.prisma.comment.count({
            where: { status: 1 }
        })
    }

    async list (query: { articleId: number, pageIndex: number, pageSize: number }) {
        const list = await this.prisma.comment.findMany({
            where: {
                articleId: query.articleId,
                status: 1
            },
            select: {
                id: true,
                nickName: true,
                content: true,
                createTime: true,
                isOwner: true
            },
            skip: (query.pageIndex - 1) * query.pageSize,
            take: query.pageSize
        })

        const total = await this.prisma.comment.count({
            where: {
                articleId: query.articleId,
                status: 1
            }
        })
        
        return { list, total }
    }

    create(createCommentDto: CreateCommentDto) {
        return this.prisma.comment.create({
            data: createCommentDto
        })
    }
}
